import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import com.alibaba.fastjson.JSONObject;
import com.classify.system.domain.ListText;
import org.junit.After;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

public class ArryText {

    static List<ListText> electronTypes = new ArrayList<>();
    @Test
    public void main() {
//一级
        //一级手机
        //TODO
        ListText electronType1 = new ListText();
        electronType1.setTid(1);
        electronType1.setTname("手机");
        electronType1.setPid(0);
        electronType1.setChildrenList(new ArrayList<ListText>());
        electronTypes.add(electronType1);

        //一级电脑
        ListText electronType2 = new ListText();
        electronType2.setTid(2);
        electronType2.setTname("电脑");
        electronType2.setPid(0);
        electronType2.setChildrenList(new ArrayList<ListText>());
        electronTypes.add(electronType2);

        //一级电视
        ListText electronType3 = new ListText();
        electronType3.setTid(3);
        electronType3.setTname("电视机");
        electronType3.setPid(0);
        electronType3.setChildrenList(new ArrayList<ListText>());
        electronTypes.add(electronType3);
        //TODO//TODO
        //二级
        //二级手机
        ListText electronType11 = new ListText();
        electronType11.setTid(4);
        electronType11.setTname("苹果手机");
        electronType11.setPid(1);
        electronType11.setChildrenList(new ArrayList<ListText>());
        electronTypes.add(electronType11);

        ListText electronType12 = new ListText();
        electronType12.setTid(5);
        electronType12.setTname("华为手机");
        electronType12.setPid(1);
        electronType12.setChildrenList(new ArrayList<ListText>());
        electronTypes.add(electronType12);

        ListText electronType13 = new ListText();
        electronType13.setTid(6);
        electronType13.setTname("小米手机");
        electronType13.setPid(1);
        electronType13.setChildrenList(new ArrayList<ListText>());
        electronTypes.add(electronType13);
        //二级电脑
        ListText electronType21 = new ListText();
        electronType21.setTid(7);
        electronType21.setTname("苹果电脑");
        electronType21.setPid(2);
        electronType21.setChildrenList(new ArrayList<ListText>());
        electronTypes.add(electronType21);

        ListText electronType22 = new ListText();
        electronType22.setTid(8);
        electronType22.setTname("华为电脑");
        electronType22.setPid(2);
        electronType22.setChildrenList(new ArrayList<ListText>());
        electronTypes.add(electronType22);

        ListText electronType23 = new ListText();
        electronType23.setTid(9);
        electronType23.setTname("小米电脑");
        electronType23.setPid(2);
        electronType23.setChildrenList(new ArrayList<ListText>());
        electronTypes.add(electronType23);
        //二级电视
        ListText electronType31 = new ListText();
        electronType31.setTid(10);
        electronType31.setTname("苹果电视");
        electronType31.setPid(3);
        electronType31.setChildrenList(new ArrayList<ListText>());
        electronTypes.add(electronType31);

        ListText electronType32 = new ListText();
        electronType32.setTid(11);
        electronType32.setTname("小米电视");
        electronType32.setPid(3);
        electronType32.setChildrenList(new ArrayList<ListText>());
        electronTypes.add(electronType32);

        ListText electronType33 = new ListText();
        electronType33.setTid(12);
        electronType33.setTname("华为电视");
        electronType33.setPid(3);
        electronType33.setChildrenList(new ArrayList<ListText>());
        electronTypes.add(electronType33);

        //TODO//TODO
        //三级
        //三级苹果手机
        ListText electronType111 = new ListText();
        electronType111.setTid(13);
        electronType111.setTname("苹果电池");
        electronType111.setPid(4);
        electronType111.setChildrenList(new ArrayList<ListText>());
        electronTypes.add(electronType111);

        ListText electronType112 = new ListText();
        electronType112.setTid(14);
        electronType112.setTname("苹果内存");
        electronType112.setPid(4);
        electronType112.setChildrenList(new ArrayList<ListText>());
        electronTypes.add(electronType112);

        ListText electronType113 = new ListText();
        electronType113.setTid(15);
        electronType113.setTname("苹果存储卡");
        electronType113.setPid(4);
        electronType113.setChildrenList(new ArrayList<ListText>());
        electronTypes.add(electronType113);
        //三级华为手机
        ListText electronType121 = new ListText();
        electronType121.setTid(16);
        electronType121.setTname("华为电池");
        electronType121.setPid(5);
        electronType121.setChildrenList(new ArrayList<ListText>());
        electronTypes.add(electronType121);

        ListText electronType122 = new ListText();
        electronType122.setTid(17);
        electronType122.setTname("华为内存");
        electronType122.setPid(5);
        electronType122.setChildrenList(new ArrayList<ListText>());
        electronTypes.add(electronType122);

        ListText electronType123 = new ListText();
        electronType123.setTid(18);
        electronType123.setTname("华为存储卡");
        electronType123.setPid(5);
        electronType123.setChildrenList(new ArrayList<ListText>());
        electronTypes.add(electronType123);
        //三级小米手机
        ListText electronType131 = new ListText();
        electronType131.setTid(19);
        electronType131.setTname("小米电池");
        electronType131.setPid(6);
        electronType131.setChildrenList(new ArrayList<ListText>());
        electronTypes.add(electronType131);

        ListText electronType132 = new ListText();
        electronType132.setTid(20);
        electronType132.setTname("小米内存");
        electronType132.setPid(6);
        electronType132.setChildrenList(new ArrayList<ListText>());
        electronTypes.add(electronType132);

        ListText electronType133 = new ListText();
        electronType133.setTid(21);
        electronType133.setTname("小米存储卡");
        electronType133.setPid(6);
        electronType133.setChildrenList(new ArrayList<ListText>());
        electronTypes.add(electronType133);

        //三级苹果电脑
        ListText electronType211 = new ListText();
        electronType211.setTid(22);
        electronType211.setTname("苹果cpu");
        electronType211.setPid(7);
        electronType211.setChildrenList(new ArrayList<ListText>());
        electronTypes.add(electronType211);

        ListText electronType212 = new ListText();
        electronType212.setTid(23);
        electronType212.setTname("苹果磁盘");
        electronType212.setPid(7);
        electronType212.setChildrenList(new ArrayList<ListText>());
        electronTypes.add(electronType212);

        ListText electronType213 = new ListText();
        electronType213.setTid(24);
        electronType213.setTname("苹果主机");
        electronType213.setPid(7);
        electronType213.setChildrenList(new ArrayList<ListText>());
        electronTypes.add(electronType213);
        //三级华为电脑
        ListText electronType221 = new ListText();
        electronType221.setTid(25);
        electronType221.setTname("华为cpu");
        electronType221.setPid(8);
        electronType221.setChildrenList(new ArrayList<ListText>());
        electronTypes.add(electronType221);

        ListText electronType222 = new ListText();
        electronType222.setTid(26);
        electronType222.setTname("华为磁盘");
        electronType222.setPid(8);
        electronType222.setChildrenList(new ArrayList<ListText>());
        electronTypes.add(electronType222);

        ListText electronType223 = new ListText();
        electronType223.setTid(27);
        electronType223.setTname("华为主机");
        electronType223.setPid(8);
        electronType223.setChildrenList(new ArrayList<ListText>());
        electronTypes.add(electronType223);
        //三级小米电脑
        ListText electronType231 = new ListText();
        electronType231.setTid(28);
        electronType231.setTname("小米cpu");
        electronType231.setPid(9);
        electronType231.setChildrenList(new ArrayList<ListText>());
        electronTypes.add(electronType231);

        ListText electronType232 = new ListText();
        electronType232.setTid(29);
        electronType232.setTname("小米磁盘");
        electronType232.setPid(9);
        electronType232.setChildrenList(new ArrayList<ListText>());
        electronTypes.add(electronType232);

        ListText electronType233 = new ListText();
        electronType233.setTid(30);
        electronType233.setTname("小米主机");
        electronType233.setPid(9);
        electronType233.setChildrenList(new ArrayList<ListText>());
        electronTypes.add(electronType233);
        //三级电视
        ListText electronType311 = new ListText();
        electronType311.setTid(31);
        electronType311.setTname("小米水晶电视");
        electronType311.setPid(11);
        electronType311.setChildrenList(new ArrayList<ListText>());
        electronTypes.add(electronType311);
        //四级
        ListText electronType1111 = new ListText();
        electronType1111.setTid(32);
        electronType1111.setTname("苹果手机不耐用的电池");
        electronType1111.setPid(13);
        electronType1111.setChildrenList(new ArrayList<ListText>());
        electronTypes.add(electronType1111);

        ListText electronType1321 = new ListText();
        electronType1321.setTid(33);
        electronType1321.setTname("小米机械硬盘");
        electronType1321.setPid(20);
        electronType1321.setChildrenList(new ArrayList<ListText>());
        electronTypes.add(electronType1321);

        ListText electronType1322 = new ListText();
        electronType1322.setTid(34);
        electronType1322.setTname("小米固态硬盘");
        electronType1322.setPid(20);
        electronType1322.setChildrenList(new ArrayList<ListText>());
        electronTypes.add(electronType1322);
    }
     // 使用@Test注解表明这是一个JUnit测试方法，@After注解表明此方法将在每个测试方法运行后执行
    @Test
    @After
    public void test2()
    {
        // 创建一个TreeNodeConfig对象，用于配置树节点
        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();

        // 使用TreeUtil工具类构建树结构，参数包括：节点列表、根节点标识、节点配置、以及一个Lambda表达式用于设置节点属性
        List<Tree<String>> build = TreeUtil.build(electronTypes, "0", treeNodeConfig, (node, treeNode) -> {
            // 设置当前树节点的ID
//            treeNode.setTid(node.getTid().toString());
            treeNode.setId(node.getTid().toString());
            // 设置当前树节点的父ID
            treeNode.setParentId(node.getPid().toString());
            // 设置当前树节点的名称
            treeNode.setName(node.getTname());
        });

        // 打印构建的树结构，使用JSONObject的toJSONString方法转换为JSON格式字符串
        System.out.println(JSONObject.toJSONString(build));
    }
}
